package cc.mrbird.febs.book.controller;


import cc.mrbird.febs.book.dto.HistoryDTO;
import cc.mrbird.febs.book.entity.Books;
import cc.mrbird.febs.book.entity.Ratings;
import cc.mrbird.febs.book.service.impl.BooksServiceImpl;
import cc.mrbird.febs.book.service.impl.RatingsServiceImpl;
import cc.mrbird.febs.book.vo.*;

import cc.mrbird.febs.common.utils.Result;
import cc.mrbird.febs.common.utils.RowsData;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author wbt
 */
@Slf4j
@Validated
@RestController
@RequestMapping(value = "books")
public class BooksController {
    @Autowired
    private BooksServiceImpl booksService;
    @Autowired
    private RatingsServiceImpl ratingsService;

    @PostMapping("deleteBookByBookId")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBookByIsbn(@RequestBody BookIdsVO bookIds) {
        for (String bookId : bookIds.getBookIds()) {
            booksService.remove(new QueryWrapper<Books>().eq("book_id", bookId));
        }
        return Result.success("操作成功");

    }


    @PostMapping("updateBook")
    public Result updateBook(@RequestBody Books books) {
        Books isbn = booksService.getOne(new QueryWrapper<Books>().eq("ISBN", books.getIsbn()));
        if (!StringUtils.isEmpty(isbn)) {
            boolean b = booksService.updateBook(books);
            if (b) {
                return Result.success("操作成功！");
            } else {
                return Result.fail("操作失败！");
            }
        } else {
            return Result.fail("没有查到该图书，请再次确认！");
        }
    }


    //详情
    @PostMapping("bookInfoById")
    public Result bookInfoById(String bookId) {
        Books books = booksService.getOne(new QueryWrapper<Books>().eq("book_id", bookId));
        if (!StringUtils.isEmpty(books)) {
            return Result.success(books, "查询成功");
        } else {
            return Result.fail("暂无数据");
        }

    }

    //前十
    @GetMapping("bookTopList")
    public Result selectuserListTop() {
        return booksService.getList();
    }

    //用户图书列表界面
    @PostMapping("userBookList")
    public Result<RowsData<Books>> selectUserBook(@RequestBody UserBookList vo) {
        return booksService.userBootList(vo);
    }

    //详情
    @GetMapping("bookInfo")
    public Result<Books> selectBookInfo(String bookId) {
        return Result.success(booksService.selectBookInfo(bookId), "查询成功");
    }

    @GetMapping("bookScore")
    public Result bookScore(String bookId, String score, String userId) {
        Books one = booksService.getOne(new QueryWrapper<Books>().eq("book_id", bookId));
        Ratings serviceOne = ratingsService.getOne(new QueryWrapper<Ratings>().eq("User_ID", userId)
                .eq("book_id", bookId));
        if (StringUtils.isEmpty(serviceOne)) {
            Ratings ratings = new Ratings();
            ratings.setBookId(one.getBookId());
            ratings.setBookRating(Integer.valueOf(score));
            ratings.setUserID(Integer.valueOf(userId));
            boolean b = ratingsService.save(ratings);
            if (b) {
                return Result.success("操作成功");
            } else {
                return Result.fail("操作失败");
            }
        } else {
            return Result.fail("你已对该书做过评价，不能再次评价！");
        }

    }

    //历史评分
    @PostMapping("historyScore")
    public Result<RowsData<HistoryDTO>> historyScore(@RequestBody HistoryVO vo) {
        return booksService.historyList(vo);
    }

    //推荐列表
    @PostMapping("getRecommendList")
    public Result<RowsData<HistoryDTO>> getRecommendList(@RequestBody HistoryVO vo) {
        return booksService.getRecommendList(vo);
    }

    //相关推荐
    @PostMapping("getBookListByAuthor")
    public Result<RowsData<HistoryDTO>> getBookListByAuthor(@RequestBody RecommendVO vo) {
        return booksService.getBookListByAuthor(vo);
    }

}
